package br.com.mayco.util;

/**
 * @author Mayco Lucian
 * @since 25/02/2013
 * @version 1.0.0
 */
public class StringUtil {

    public static final String EMPTY = "";

    public StringUtil() {
    }

    public static String invert(String string) {
        StringBuilder sb = new StringBuilder();
        for (int i = string.length(); i >= 0; i--) {
            sb.append(string.charAt(i));
        }
        return sb.toString();
    }

    public static String removeDupSpaces(String string) {
        string = string.trim();
        String res = string;
        do {
            res = res.replace("  ", " ");
        } while (res.contains("  "));
        return res;
    }

    public static String firstWord(String string) {
        string = removeDupSpaces(string);
        String[] items = string.split(" ");
        if (items.length > 0) {
            return items[0];
        }
        return string;
    }

    public static String lastWord(String string) {
        string = removeDupSpaces(string);
        String[] items = string.split(" ");
        if (items.length > 0) {
            return items[items.length - 1];
        }
        return string;
    }

    public static Boolean isEmpty(String string) {
        return string.length() == 0;
    }

    public static Boolean isNullOrEmpty(String string) {
        return string == null || string.length() == 0;
    }

    public static String normalize(String str) {
        if (isNullOrEmpty(str)) {
            return str;
        }
        str = str.toLowerCase();
        str = str.replace(' ', '_');
        str = str.replace('/', '_');
        str = str.replace(':', '_');
        str = str.replace('\\', '_');
        str = str.replace('|', '_');
        str = str.replace('á', 'a');
        str = str.replace('à', 'a');
        str = str.replace('â', 'a');
        str = str.replace('ã', 'a');
        str = str.replace('ç', 'c');
        str = str.replace('é', 'e');
        str = str.replace('ê', 'e');
        str = str.replace('í', 'i');
        str = str.replace('î', 'i');
        str = str.replace('ì', 'i');
        str = str.replace('ó', 'o');
        str = str.replace('ô', 'o');
        str = str.replace('õ', 'o');
        str = str.replace('û', 'u');
        str = str.replace('ú', 'u');
        str = str.replace('ü', 'u');
        return str;
    }
}
